package com.tqjc.plccontrol.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.execute.task.entity.Task;
import com.tqjc.execute.task.mapper.TaskMapper;
import com.tqjc.plccontrol.entity.PointInfo;
import com.tqjc.plccontrol.mapper.PointInfoMapper;
import com.tqjc.plccontrol.service.PointInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @projectName: BaotouSteel
 * @className: PointInfoServiceImpl
 * @author: Patienyon
 * @description: 点位信息服务实现类
 * @date: 2023/6/13 10:19
 */

@Service
public class PointInfoServiceImpl extends ServiceImpl<PointInfoMapper, PointInfo> implements PointInfoService {
    @Resource
    private PointInfoMapper pointInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public PointInfo queryById(Integer id){
        return pointInfoMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param pointInfo 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PointInfo> paginQuery(PointInfo pointInfo, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PointInfo> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(pointInfo.getPlcSn())){
            queryWrapper.eq(PointInfo::getPlcSn, pointInfo.getPlcSn());
        }
        if(StrUtil.isNotBlank(pointInfo.getConnectSn())){
            queryWrapper.eq(PointInfo::getConnectSn, pointInfo.getConnectSn());
        }
        if(StrUtil.isNotBlank(pointInfo.getName())){
            queryWrapper.eq(PointInfo::getName, pointInfo.getName());
        }
        if(StrUtil.isNotBlank(pointInfo.getAddress())){
            queryWrapper.eq(PointInfo::getAddress, pointInfo.getAddress());
        }
        //2. 执行分页查询
        Page<PointInfo> pagin = new Page<>(current , size , true);
        IPage<PointInfo> selectResult = pointInfoMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param pointInfo 实例对象
     * @return 实例对象
     */
    public PointInfo insert(PointInfo pointInfo){
        pointInfoMapper.insert(pointInfo);
        return pointInfo;
    }

    /**
     * 更新数据
     *
     * @param pointInfo 实例对象
     * @return 实例对象
     */
    public PointInfo update(PointInfo pointInfo){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PointInfo> chainWrapper = new LambdaUpdateChainWrapper<PointInfo>(pointInfoMapper);
        if(StrUtil.isNotBlank(pointInfo.getPlcSn())){
            chainWrapper.eq(PointInfo::getPlcSn, pointInfo.getPlcSn());
        }
        if(StrUtil.isNotBlank(pointInfo.getConnectSn())){
            chainWrapper.eq(PointInfo::getConnectSn, pointInfo.getConnectSn());
        }
        if(StrUtil.isNotBlank(pointInfo.getName())){
            chainWrapper.eq(PointInfo::getName, pointInfo.getName());
        }
        if(StrUtil.isNotBlank(pointInfo.getAddress())){
            chainWrapper.eq(PointInfo::getAddress, pointInfo.getAddress());
        }
        //2. 设置主键，并更新
        chainWrapper.set(PointInfo::getId, pointInfo.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(pointInfo.getId());
        }else{
            return pointInfo;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = pointInfoMapper.deleteById(id);
        return total > 0;
    }
}
